﻿using DevExpress.Utils;
using DevExpress.XtraEditors.Controls;
using DevExpress.XtraEditors.Repository;
using DevExpress.XtraGrid.Views.Grid;
using RecruitStudents.Utility;
using RecruitStudents.Utility.Const;
using RecruitStudents.Utility.Helper;
using RecruitStudents.Utility.Model;
using RecruitStudents.Utility.Push;
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace RecruitStudents.Client.Examinee
{
    public partial class Examinee_SpecialityExameeSearchForm_List : BaseForm
    {
        BLL.ServiceComm serviceComm = new BLL.ServiceComm();
        BLL.ServiceExaminee serviceExam = new BLL.ServiceExaminee();
        public Examinee_SpecialityExameeSearchForm_List()
        {
            InitializeComponent();
            InitData();
            //gridView1.TopRowChanged += gridView1_TopRowChanged;
        }
        #region 初始页面
        private void InitData()
        {
            #region 绑定考点列表
            DataTable dtExamCity = serviceComm.GetExamPlaceTable();
            DataRow dr = dtExamCity.NewRow();
            dr["ExamPlaceNO"] = "";
            dr["ExamPlaceName"] = "";
            dtExamCity.Rows.InsertAt(dr, 0);

            txtExamCity.Properties.DataSource = dtExamCity;
            txtExamCity.Properties.Columns.Clear();
            txtExamCity.Properties.Columns.Add(new LookUpColumnInfo("ExamPlaceNO", 30, "考点代码"));
            txtExamCity.Properties.Columns.Add(new LookUpColumnInfo("ExamPlaceName", 55, "考点名称"));
            txtExamCity.Properties.ValueMember = "ExamPlaceNO";
            txtExamCity.Properties.DisplayMember = "ExamPlaceName";
            txtExamCity.Properties.NullText = "";
            txtExamCity.EditValue = SessionerHelper.ExamPlaceID;
            //txtExamCity.EditValue = strExamCity;
            #endregion

            #region 绑定专业
            DataTable dtSpecialityKinds = new DataTable();
            dtSpecialityKinds.Columns.Add("SpecialityKindsNO");
            dtSpecialityKinds.Columns.Add("SpecialityKindsName");
            dtSpecialityKinds.Rows.Add(new object[] { "'1303010_A','1303010_B','1303010_C','1303010_D'", "表演" });
            dtSpecialityKinds.Rows.Add(new object[] { "'1303090_A','1303090_B','1303090_C','1303090_D'", "播音" });


            //DataTable dtSpecialityKinds = serviceComm.GetSpecialityKindsTable(SessionerHelper.SystemYear);
            //DataRow drpecialityKinds = dtSpecialityKinds.NewRow();
            //drpecialityKinds["SpecialityKindsNO"] = "";
            //drpecialityKinds["SpecialityKindsName"] = "";
            //dtSpecialityKinds.Rows.InsertAt(drpecialityKinds, 0);
            comboSpecialityKindsNO.Properties.DataSource = dtSpecialityKinds;
            comboSpecialityKindsNO.Properties.Columns.Clear();
            comboSpecialityKindsNO.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsNO", 30, "专业代码"));
            comboSpecialityKindsNO.Properties.Columns.Add(new LookUpColumnInfo("SpecialityKindsName", 55, "专业名称"));
            comboSpecialityKindsNO.Properties.ValueMember = "SpecialityKindsNO";
            comboSpecialityKindsNO.Properties.DisplayMember = "SpecialityKindsName";
            comboSpecialityKindsNO.Properties.NullText = "";
            comboSpecialityKindsNO.Properties.Columns[0].Visible = false;
            #endregion

            #region  绑定级别

            comboExamLevel.Properties.Columns.Clear();
            //comboExamLevel.Properties.Columns.Add(new LookUpColumnInfo("Key", 30, "级别编码"));
            comboExamLevel.Properties.Columns.Add(new LookUpColumnInfo("Value", "级别名称"));
            comboExamLevel.Properties.DataSource = ConstExamLevel.DicExamLevel_ALL.ToList();
            comboExamLevel.Properties.ValueMember = "Key";
            comboExamLevel.Properties.DisplayMember = "Value";
            comboExamLevel.Properties.NullText = "";
            #endregion

            #region 绑定缴费来源
            //this.comboPaySource.Properties.NullText = "";
            ////this.comboPaySource.Properties.Columns.Add(new LookUpColumnInfo("Key", 30, "缴费编号"));
            //this.comboPaySource.Properties.Columns.Add(new LookUpColumnInfo("Value", "缴费来源"));
            //this.comboPaySource.Properties.DataSource = ConstPaySource.DicPaySource.ToList();
            //this.comboPaySource.Properties.DisplayMember = "Value";
            //this.comboPaySource.Properties.ValueMember = "Key";
            #endregion

            #region 绑定是否合格
            //this.cmbIsPass.Properties.NullText = "";
            ////this.cmbIsPass.Properties.Columns.Add(new LookUpColumnInfo("Key", 30, "编号"));
            //this.cmbIsPass.Properties.Columns.Add(new LookUpColumnInfo("Value", "是否合格"));
            //this.cmbIsPass.Properties.DataSource = ConstAppConfig.DicIsPass.ToList();
            //this.cmbIsPass.Properties.DisplayMember = "Value";
            //this.cmbIsPass.Properties.ValueMember = "Key";
            #endregion

            #region 绑定合格证是否已下载
            //this.cmbIsDownload.Properties.NullText = "";
            ////this.cmbIsDownload.Properties.Columns.Add(new LookUpColumnInfo("Key", 30, "合格证下载编号"));
            //this.cmbIsDownload.Properties.Columns.Add(new LookUpColumnInfo("Value", "合格证下载"));
            //this.cmbIsDownload.Properties.DataSource = ConstAppConfig.DicIsDownload.ToList();
            //this.cmbIsDownload.Properties.DisplayMember = "Value";
            //this.cmbIsDownload.Properties.ValueMember = "Key";
            #endregion

            BindAppointmentGroup("", "", ""); //绑定考试分组
        }
        #endregion

        #region 绑定考试分组
        private void BindAppointmentGroup(string queryExamCity, string querySpecialityKindsNO, string queryExamLevel)
        {
            comboExamRoom.Properties.DataSource = null;
            comboExamRoom.Properties.NullText = "";
            comboExamRoom.EditValue = null;
            string ExamPlaceID = string.Empty;
            string ExamPlaceName = string.Empty;
            if (txtExamCity.EditValue != null)
            {
                ExamPlaceID = txtExamCity.EditValue.ToSafeString();
            }
            string SpecialityKindsNO = string.Empty;
            if (comboSpecialityKindsNO.EditValue != null)
            {
                SpecialityKindsNO = comboSpecialityKindsNO.EditValue.ToSafeString();
            }
            string ExamLevel = string.Empty;
            if (comboExamLevel.EditValue != null)
            {
                ExamLevel = comboExamLevel.EditValue.ToSafeString();
            }
            if (ExamPlaceID.IsNullOrEmptyString() || SpecialityKindsNO.IsNullOrEmptyString() || ExamLevel.IsNullOrEmptyString())
            {
                return;
            }
            using (DataTable dt = serviceComm.GetExamRoomByPlaceID_Speciality_Level_CS(ExamPlaceID, SpecialityKindsNO, ExamLevel))
            {
                comboExamRoom.Properties.DataSource = dt;
                comboExamRoom.Properties.Columns.Clear();
                comboExamRoom.Properties.Columns.Add(new LookUpColumnInfo("ExamRoomNO", 30, "考场编号"));
                comboExamRoom.Properties.Columns.Add(new LookUpColumnInfo("ExamRoomName", 55, "考场名称"));
                comboExamRoom.Properties.ValueMember = "ExamRoomNO";
                comboExamRoom.Properties.DisplayMember = "ExamRoomName";
                comboExamRoom.Properties.NullText = "";
            }
            //DataTable dtAppointmentGroup = serviceComm.GetAppointmentGroupDataTable(queryExamCity, querySpecialityKindsNO, queryExamLevel);
            //comboAppointmentGroup.Properties.DataSource = dtAppointmentGroup;
            //comboAppointmentGroup.Properties.Columns.Clear();
            //comboAppointmentGroup.Properties.Columns.Add(new LookUpColumnInfo("GroupNo", 30, "考试分组编号"));
            //comboAppointmentGroup.Properties.Columns.Add(new LookUpColumnInfo("GroupName", 55, "考试分组名称"));
            //comboAppointmentGroup.Properties.ValueMember = "GroupNo";
            //comboAppointmentGroup.Properties.DisplayMember = "GroupName";
            //comboAppointmentGroup.Properties.NullText = "";
        }
        #endregion

        #region 绑定gridControl1
        DataTable dtDataSource = null;
        private void BindInfo(int currentIndexData)
        {
            DataTable dt = serviceExam.GetSpecialityExameeSearchDataTable(txtExamCity.EditValue.ToSafeString(), comboSpecialityKindsNO.EditValue.ToSafeString(),
               comboExamLevel.EditValue.ToSafeString(), txtName.Text, txtIdCard.Text, txtNMT_SignUpID.Text, comboExamRoom.EditValue.ToSafeString(), SessionerHelper.SystemYear);
            if (dt != null)
            {
                dt.Columns.Add("ExamState", typeof(bool));
                if (dtDataSource == null)
                {
                    dtDataSource = dt.Clone();
                }
                foreach (DataRow dr in dt.Rows)
                {
                    if (dr["Sex"].ToString() == "1")
                    {
                        dr["Sex"] = "男";
                    }
                    else
                    {
                        dr["Sex"] = "女";
                    }
                    if (dr["ExamStateNew"].ToString() == "1")
                    {
                        dr["ExamState"] = true;
                    }
                    else
                    {
                        dr["ExamState"] = false;
                    }
                    dtDataSource.ImportRow(dr);
                }
            }
            gridControl1.DataSource = dtDataSource;
            //gridView1.TopRowChanged -= gridView1_TopRowChanged;
            if (dt != null)
            {
                currentCount = dtDataSource.Rows.Count;
                //if (dt.Rows.Count == pageSize)
                //{
                //    gridView1.TopRowChanged += gridView1_TopRowChanged;
                //}
            }
            this.gridView1.ClearSelection();
            //decimal feeMoney = 0;
            //DataTable dt = serviceExam.GetExamineeOfAllInfo(txtExamCity.EditValue.ToSafeString(), comboSpecialityKindsNO.EditValue.ToSafeString(), comboAppointmentGroup.EditValue.ToSafeString(),
            //    string.Empty, string.Empty, string.Empty, comboExamLevel.EditValue.ToSafeString(),
            //    txtName.Text, txtNMT_SignUpID.Text, txtIdCard.Text, string.Empty, currentIndexData.ToSafeString(), pageSize.ToSafeString(), out totalCount, out feeMoney);
            //if (dt != null)
            //{
            //    if (dtDataSource == null)
            //    {
            //        dtDataSource = dt.Clone();
            //    }
            //    foreach (DataRow dr in dt.Rows)
            //    {
            //        if (dr["Sex"].ToString() == "1")
            //        {
            //            dr["Sex"] = "男";
            //        }
            //        else
            //        {
            //            dr["Sex"] = "女";
            //        }
            //        dtDataSource.ImportRow(dr);
            //    }
            //}
            //gridControl1.DataSource = dtDataSource;
            //gridView1.TopRowChanged -= gridView1_TopRowChanged;
            //if (dt != null)
            //{
            //    currentCount = dtDataSource.Rows.Count;
            //    if (dt.Rows.Count == pageSize)
            //    {
            //        gridView1.TopRowChanged += gridView1_TopRowChanged;
            //    }
            //}
            //this.gridView1.ClearSelection();
        }
        #endregion

        int currentIndex = 1;
        int currentCount = -1;
        int totalCount = -1;
        int pageSize = 50;
        private void gridView1_TopRowChanged(object sender, EventArgs e)
        {
            //if (gridView1.IsRowVisible(currentCount - 10) == DevExpress.XtraGrid.Views.Grid.RowVisibleState.Visible)
            //{
            //    int MaxPageIndex = ((totalCount - 1) / pageSize) + 1;
            //    int pageIndex = ++currentIndex;
            //    if (pageIndex > MaxPageIndex)
            //    {
            //        return;
            //    }
            //    BindInfo(pageIndex);
            //}
        }
        protected override void OnLoad(EventArgs e)
        {
            gridView1.IndicatorWidth = 40;
            gridView1.PreviewIndent = 0;
            gridView1.CustomDrawRowIndicator += gridView1_CustomDrawRowIndicator;
            base.OnLoad(e);
        }

        void gridView1_CustomDrawRowIndicator(object sender, RowIndicatorCustomDrawEventArgs e)
        {
            e.Appearance.TextOptions.HAlignment = HorzAlignment.Far;
            if (e.Info.IsRowIndicator && e.RowHandle >= 0)
            {
                e.Info.DisplayText = (e.RowHandle + 1).ToString();
            }
        }
        #region 下拉框选择事件
        private void txtExamCity_EditValueChanged(object sender, EventArgs e)
        {
            BindAppointmentGroup("", "", ""); //绑定考试分组
            //if (txtExamCity.Text != "" && comboSpecialityKindsNO.Text != "" && comboExamLevel.Text != "")
            //{
            //    BindAppointmentGroup(txtExamCity.EditValue.ToString(), comboSpecialityKindsNO.EditValue.ToString(), comboExamLevel.EditValue.ToString());
            //}
        }

        private void comboSpecialityKindsNO_EditValueChanged(object sender, EventArgs e)
        {
            BindAppointmentGroup("", "", ""); //绑定考试分组
            //if (txtExamCity.Text != "" && comboSpecialityKindsNO.Text != "" && comboExamLevel.Text != "")
            //{
            //    BindAppointmentGroup(txtExamCity.EditValue.ToString(), comboSpecialityKindsNO.EditValue.ToString(), comboExamLevel.EditValue.ToString());
            //}
        }

        private void comboExamLevel_EditValueChanged(object sender, EventArgs e)
        {
            BindAppointmentGroup("", "", ""); //绑定考试分组
            //if (txtExamCity.Text != "" && comboSpecialityKindsNO.Text != "" && comboExamLevel.Text != "")
            //{
            //    BindAppointmentGroup(txtExamCity.EditValue.ToString(), comboSpecialityKindsNO.EditValue.ToString(), comboExamLevel.EditValue.ToString());
            //}
        }
        #endregion

        private void gridView1_DoubleClick(object sender, EventArgs e)
        {
            if (gridView1.GetFocusedValue() != null)
            {
                GridView rows = sender as GridView;
                int[] inteurm = rows.GetSelectedRows();
                if (inteurm != null && inteurm.Length > 0)
                {
                    string strNMT_SignUpID = rows.Rows[inteurm[0]].DataRow["NMT_SignUpID"].ToString();
                    Examinee_ExameeDetailForm obj = new Examinee_ExameeDetailForm();
                    obj.strNMT_SignUpID = strNMT_SignUpID;
                    obj.strType = "Look";
                    obj.Run();
                }
            }
        }

        #region 导出execl
        private void btnImportExcel_Click(object sender, EventArgs e)
        {
            if (comboExamRoom.EditValue == null || comboExamRoom.EditValue.ToSafeString() == "")
            {
                DevHelper.ShowErrorMessageBox("请先选择考场进行导出!");
                return;
            }
            int[] selectInt = gridView1.GetSelectedRows();
            if (selectInt == null || selectInt.Length == 0)
            {
                DevHelper.ShowErrorMessageBox("请先选择需要导出的考生！");
                return;
            }
            DataTable dt = serviceExam.GetSignUpStudentsByExcel_New(comboExamRoom.EditValue.ToSafeString());
            if (dt == null)
            {
                DevHelper.ShowErrorMessageBox("暂时还没有报考专业信息,无法进行导出!");
                return;
            }
            else
            {
                DataTable dtNew = new DataTable();
                dtNew = dt.Clone();
                foreach (int len in selectInt)
                {
                    foreach (DataRow dr in dt.Rows)
                    {
                        if (gridView1.GetRowCellValue(len, gridView1.Columns["IdentityCard"]).ToString() == dr["IdentityCard"].ToSafeString())
                        {
                            dtNew.ImportRow(dr);
                        }
                    }
                }
                string strSpecNo = comboSpecialityKindsNO.EditValue.ToString();
                string strTemplateFile = Application.StartupPath + "\\Template\\考官打分情况.xls";
                if (System.IO.File.Exists(strTemplateFile))
                {
                    SaveFileDialog kk = new SaveFileDialog();
                    kk.Title = "保存EXECL文件";
                    kk.Filter = "EXECL文件(*.xls) |*.xls |所有文件(*.*) |*.*";
                    kk.FilterIndex = 1;
                    kk.FileName = txtExamCity.Text + comboSpecialityKindsNO.Text + comboExamLevel.Text + "考生打分表" + DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";
                    kk.ShowDialog();
                    if (kk.FileName.IndexOf(":") < 0) return; //被点了取消 

                    string saveFileName = kk.FileName;
                    //string title = DateTime.Now.Year.ToSafeString() + "年" + SessionerHelper.SystemSchoolName + "招生考试";

                    NPOIHelper.ListColumnsName = new SortedList(new NoSort());
                    NPOIHelper.ListColumnsName.Add("AllowExamID", "准考证号");
                    NPOIHelper.ListColumnsName.Add("Name", "姓名");
                    NPOIHelper.ListColumnsName.Add("NMT_SignUpID", "考生号");
                    NPOIHelper.ListColumnsName.Add("IdentityCard", "身份证号");
                    NPOIHelper.ListColumnsName.Add("sec", "性别");
                    NPOIHelper.ListColumnsName.Add("DicName", "生源地");
                    NPOIHelper.ExportExcelForDtByTemplate(dtNew, strTemplateFile, saveFileName, 1);
                    btnImportExcel.Enabled = false;
                }
                else
                {
                    DevHelper.ShowErrorMessageBox("不存在对应导出的专业模板,无法进行导出!");
                }
            }
        }
        #endregion

        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
            this.Close();
        }

        private void Method()
        {
            if (listNotice != null)
            {
                foreach (TB_Notice model in listNotice)
                {
                    #region 推送消息
                    PushHelper objJPush = new PushHelper();
                    TB_ConsultTitle objTitle = new TB_ConsultTitle();
                    TB_ConsultReply objReply = new TB_ConsultReply();
                    objTitle.UsersID = model.SignUpID;
                    objTitle.ID = model.NoticeID;
                    objTitle.ConsultTitle = "通知公告";
                    objTitle.TypeID = ConsultType.TZ;
                    objJPush.Push(objTitle, objReply);
                    #endregion
                }
            }
            listNotice = null;
        }

        List<TB_Notice> listNotice = null;
        #region 发送
        private void btnSendSMS_Click(object sender, EventArgs e)
        {
            //if (txtNoticeTitle.Text.Length == 0)
            //{
            //    txtNoticeTitle.Focus();
            //    DevHelper.ShowErrorMessageBox("请输入消息标题！");
            //    return;
            //}
            //if (memoMessage.Text.Length == 0)
            //{
            //    memoMessage.Focus();
            //    DevHelper.ShowErrorMessageBox("请输入消息内容！");
            //    return;
            //}
            //int[] selectInt = gridView1.GetSelectedRows();
            //if (selectInt == null || selectInt.Length == 0)
            //{
            //    DevHelper.ShowErrorMessageBox("请先选择需要接送消息的考生！");
            //    return;
            //}
            //listNotice = new List<TB_Notice>();
            //foreach (int len in selectInt)
            //{
            //    TB_Notice notice = new TB_Notice();
            //    notice.SignUpID = gridView1.GetRowCellValue(len, gridView1.Columns["SignUpID"]).ToString(); ;
            //    notice.NoticeType = "private";
            //    notice.NoticeTitle = txtNoticeTitle.Text;
            //    notice.Message = memoMessage.Text;
            //    notice.IsRead = "0";
            //    notice.CreateTime = DateTime.Now;
            //    notice.IsActive = "1";
            //    listNotice.Add(notice);
            //}
            //if (listNotice.Count > 0)
            //{
            //    if (new BLL.ServiceConsult().SaveNoticeList(listNotice))
            //    {
            //        Thread thread = new Thread(new ThreadStart(Method));
            //        thread.Start();
            //    }
            //    txtNoticeTitle.Text = "";
            //    memoMessage.Text = "";
            //}
        }
        #endregion

        private void btnQuery_Click(object sender, EventArgs e)
        {
            dtDataSource = null;
            BindInfo(1);
            btnImportExcel.Enabled = true;
        }
    }
}
